'''
Created on Oct 11, 2012

@author: daeheehan
'''
from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
import sys

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"
GITHUB_USER_ID_3 = 'luispigu'
GITHUB_USER_ID_4 = 'skswhwo'

GITHUB_USER_PW = "qwer1234"
GITHUB_USER_PW_1 = "wasd1234"

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID_1, password=GITHUB_USER_PW)
print gh.ratelimit_remaining, 'request remaining'

# Github node Repository information
GITHUB_USER_NAME = 'joyent'
GITHUB_REPOSITORY_NAME = 'node'

# Node collaborators information file
COLLABORATORS_FILE = 'node_collaborators.txt'

# Node collaborator followers information file
COLLABORATORS_FOLLOWER_FILE = 'node_collaborators_follower.txt'

project_collaborators = []
collaborators_follower = {}

try:
    # Github oh-my-zsh project collaborator list from files
    f_collaborators = open(COLLABORATORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_collaborators.append(user_login)

    f_collaborators.close()
    
    print 'loading collaborator file is finished'

    for item in project_collaborators:
        print 'login id: ' +  item
        user = gh.user(item)

#        print user.followers
        collaborators_follower[item] = user.followers
        
        if(gh.ratelimit_remaining < 100):
            time.sleep(60 * 60)
#        break


    f_collaborators_follower = open(COLLABORATORS_FOLLOWER_FILE, 'w')
    print 'Printing dictionary: collaborator followers'
    for key, value in collaborators_follower.items():
        sys.stdout.write(key + ", " + str(value) + '\n')
        f_collaborators_follower.write(key + ", " + str(value) + '\n')

    f_collaborators_follower.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

print
print 'Finally the end'